function drawcuboid( img, iposr, iposc, ix, iy, iz, posx, posy, posz, xscale, yscale, zscale, yrev )

side = img(iposr:iposr + iz - 1,iposc - ix:iposc + iy * 2 + ix - 1,:);
gxc = posx + ix / 2;
gx = ((posx:posx + ix) - gxc) * xscale + gxc;
sidegx = ones(iz + 1, 1) * [gx, ones(1, iy - 1) * gx(end), gx(end:-1:1), ones(1, iy) * gx(1)];
if yrev
    gyc = posy - iy / 2;
    gy = ((posy:-1:posy - iy) - gyc) * yscale + gyc;
else
    gyc = posy + iy / 2;
    gy = ((posy:posy + iy) - gyc) * yscale + gyc;
end
sidegy = ones(iz + 1, 1) * [ones(1, ix) * gy(1), gy, ones(1, ix - 1) * gy(end), gy(end:-1:1)];
gzc = posz + iz / 2;
gz = ((posz:posz + iz) - gzc) * zscale + gzc;
sidegz = gz(end:-1:1)' * ones(1, (ix + iy) * 2 + 1);

top = img(iposr - ix:iposr - 1,iposc:iposc + iy - 1,:);
bottom = img(iposr - ix:iposr - 1,iposc + iy:iposc + iy * 2 - 1,:);
tbgx = gx' * ones(1, iy + 1);
tbgy = ones(ix + 1, 1) * gy;
topgz = ones(ix + 1, iy + 1) * gz(end);
bottomgz = ones(ix + 1, iy + 1) * gz(1);

hold on;
surf(sidegx, sidegy, sidegz, side(:,:,1:end - 1), 'FaceAlpha', 'flat', 'AlphaData', double(side(:,:,end)) / 255, 'AlphaDataMapping', 'none', 'EdgeColor', 'none');
surf(tbgx, tbgy, topgz, top(:,:,1:end - 1), 'FaceAlpha', 'flat', 'AlphaData', double(top(:,:,end)) / 255, 'AlphaDataMapping', 'none', 'EdgeColor', 'none');
surf(tbgx, tbgy, bottomgz, bottom(:,:,1:end - 1), 'FaceAlpha', 'flat', 'AlphaData', double(bottom(:,:,end)) / 255, 'AlphaDataMapping', 'none', 'EdgeColor', 'none');